/**
 * Created by Administrator on 2019/2/24.
 */
var autoLogin;
$(function () {
    $.extend($.messager.defaults,{
        ok:"确定",
        cancel:"取消"
    });
    $('#autoLogin').switchbutton({
        checked: false,
        onChange: function(checked){
            autoLogin = checked;
        }
    });
    $("#login").click(function () {
        if($("input[name='username']").val()=="") {
            $.messager.alert('提示','请输入账号！','warning');
            return;
        } else if($("input[name='pwd']").val()==""){
            $.messager.alert('提示','请输入密码！','warning');
            return;
        }
        //$('#loginForm').form('submit');
        var param = new Object();
        param.linkman = $("input[name='username']").val();
        param.pwd = $("input[name='pwd']").val();
        var jsonstr = postSyncAjax("/user/loginSys",param);
        var json = jsonstr.data;
        //console.log(json);
        if(jsonstr.success) {
            $.messager.alert('提示',json.nickname+",欢迎登陆！",
                'info',function () {
                    window.top.location.href = "/main";
                });
            /*showDialogWithBtn(json.nickname+",欢迎登陆！",function () {
                window.top.location.href = "/main";
            });*/

        } else {
            $.messager.alert('提示','账号或者密码错误！','warning');
            return;
        }
    });
    //注册
    $("#register").click(function () {
        var linkman = $("input[name='linkman']").val();
        if(linkman == "") {
            $.messager.alert('提示','请输入账号！','warning');
            return;
        }
        if(linkman.length < 3 || linkman.length > 11) {
            $.messager.alert('提示','请输入3到11位账号！','warning');
            return;
        }
        var nickname = $("input[name='nickname']").val();
        if(nickname == "") {
            $.messager.alert('提示','请输入昵称！','warning');
            return;
        }
        if(nickname.length < 3 || nickname.length > 6) {
            $.messager.alert('提示','请输入3到6位昵称！','warning');
            return;
        }
        var pwd1 = $("input[name='pwd1']").val();
        if(pwd1 == "") {
            $.messager.alert('提示','请输入密码！','warning');
            return;
        }
        if(pwd1.length < 3 || pwd1.length > 20) {
            $.messager.alert('提示','请输入3到20位密码！','warning');
            return;
        }
        var pwd2 = $("input[name='pwd2']").val();
        if(pwd2.length != pwd2.length) {
            $.messager.alert('提示','两次输入密码要一致！','warning');
            return;
        }
        var param = new Object();
        param.linkman = linkman;
        param.pwd = pwd1;
        param.departmentId = $("input[name='departmentId']").val();
        param.nickname = nickname;

        var jsonstr = postSyncAjax("/user/regUser",param);
        var json = jsonstr.data;
        if(jsonstr!=null && jsonstr.success) {
            $.messager.alert('提示','注册成功！','info', function () {
                $("input[name='linkman']").val("");
                $("input[name='nickman']").val("");
                $("input[name='pwd1']").val("");
                $("input[name='pwd2']").val("");
                $(".tologin>a").click();
            });

        } else {
            $.messager.alert('提示',"注册失败["+jsonstr.msg+"]！",'warning');
            return;
        }
        //$('#registerForm').form('submit');
    });
    // 判断账号是否可用
    $("#linkman").blur(function () {
        if($("#linkman").val() == "") return;
        var param = new Object();
        param.linkman = $("input[name='linkman']").val();

        var jsonstr = postSyncAjax("/user/checkLinkman",param);
        var json = jsonstr.data;
        //console.log(jsonstr.success);
        if(jsonstr!=null && jsonstr.success) {
            //$("#linkman+span").text("账号可用").show();
            $("#linkman").prev().removeClass("no-span").addClass("ok-span");
        } else {
            //$("#linkman+span").text("").hide();
            $("#linkman").prev().removeClass("ok-span").addClass("no-span");
        }
    });
});